package com.xuanmiao.smsback.web.controller.operator;

import com.xuanmiao.smsback.domain.qo.BusinessPriceInfoQO;
import com.panshi.hujin2.base.common.factory.MessageFactory;
import com.panshi.hujin2.base.common.util.DozerUtils;
import com.panshi.hujin2.base.domain.result.BasicResult;
import com.panshi.hujin2.base.domain.result.BasicResultCode;
import com.xuanmiao.smsback.domain.qo.PriceHistoryInfoQO;
import com.xuanmiao.smsback.web.controller.vo.BusinessPriceInfoVO;

import com.xuanmiao.smsback.model.BusinessPriceInfoBO;
import com.xuanmiao.smsback.service.platform.operator.BusinessPriceInfoService;

import io.swagger.annotations.*;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@Api("商户管理")
@RequestMapping(value = "/businessPriceInfo")
@RestController
public class BusinessPriceInfoController {
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());
    @Autowired
    private BusinessPriceInfoService businessPriceInfoService;

    @ApiOperation("商户报价管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessNo" ,value = "商户号" ,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "businessArea" ,value = "营运区域 0-印尼" ,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "status" ,value = "状态0-停用 1-激活" ,dataType = "Integer",paramType = "query"),


    })
    @GetMapping()
    BasicResult<List<BusinessPriceInfoVO>> all(@ApiIgnore BusinessPriceInfoQO businessPriceInfoQO){

        try {
            List<BusinessPriceInfoBO> listBOs= businessPriceInfoService.getList(businessPriceInfoQO);

            List<BusinessPriceInfoVO> listVOs = DozerUtils.convertList(listBOs, BusinessPriceInfoVO.class);

            return BasicResult.ok(listVOs,businessPriceInfoQO.getPage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
             return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("主键查询商户报价数据")
    @GetMapping("{id:\\d+}")
    BasicResult<BusinessPriceInfoVO> queryPrimaryKey(@PathVariable Integer id) {
        try {
            BusinessPriceInfoBO res = businessPriceInfoService.getById(id);
            return BasicResult.ok(DozerUtils.convert(res, BusinessPriceInfoVO.class));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("商户号查询商户报价历史数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createTimeStart" ,value = "创建时间开始" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd" ,value = "创建时间结束" ,dataType = "Date",paramType = "query"),
            @ApiImplicitParam(name = "businessId" ,value = "商户id" ,dataType = "int",paramType = "query"),
    })
    @GetMapping("/history")
    BasicResult<List<BusinessPriceInfoVO>> queryHistory(@ApiIgnore PriceHistoryInfoQO priceHistoryInfoQO) {
        try {
            List<BusinessPriceInfoBO> listBOs= businessPriceInfoService.getHistory(priceHistoryInfoQO);
            List<BusinessPriceInfoVO> listVOs = Collections.emptyList();
            if (listBOs.size() > 0) {
                listVOs = DozerUtils.convertList(listBOs, BusinessPriceInfoVO.class);
                listVOs.stream().forEach(listVO -> {
                    listVO.setCreateTimeStr(DateFormatUtils.format(listVO.getCreateTime(), "yyyy/MM/dd HH:mm"));
                });
            }
            return BasicResult.ok(listVOs,priceHistoryInfoQO.getPage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.QUERY_FAIL.getMessage()));
        }
    }
    @ApiOperation("商户信息更新-编辑报价")
    @PutMapping("/update")
    BasicResult<Void> update(@Valid @RequestBody BusinessPriceInfoVO businessPriceInfoVO,
                             @ApiParam("用户id") @RequestHeader("sms-uid") Integer userId) {
        try {
            businessPriceInfoVO.setUpdatorId(userId);
            BusinessPriceInfoBO res = businessPriceInfoService.getById(businessPriceInfoVO.getId());
            if (res == null) {
                return BasicResult.error(BasicResultCode.DATA_NOT_EXIST.getCode(), MessageFactory.getMsg(BasicResultCode.DATA_NOT_EXIST.getMessage()));
            }
            businessPriceInfoService.update(DozerUtils.convert(businessPriceInfoVO, BusinessPriceInfoBO.class));
            return BasicResult.ok();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.UPDATE_FAIL.getCode(), MessageFactory.getMsg(BasicResultCode.UPDATE_FAIL.getMessage()));
        }
    }
}
